﻿using System;
using System.Collections;
using System.Collections.Generic;
using Library.WebControls.SimpleTreeView;
using System.IO;

namespace Library.WebControls.SimpleFileExplorer {
	public class SpFileStruct : IFileStruct {
		public SpFileStruct() {
			_child = new List<IFileStruct>();
		}
		public string Name { get; set; }
		public long Size { get; set; }
		public string ParentFolder { get; set; }
		public SpFileType FileType { get; set; }
		private readonly List<IFileStruct> _child;
		public IEnumerable<IFileStruct> Child {
			get { return _child; }
		}


		public string Key {
			get {
				if (String.IsNullOrEmpty(ParentFolder)) return Name;
				return Path.Combine(ParentFolder, Name);
			}
		}

		public DataItemState State { get; set; }

		public object DataItem {
			get { return this; }
		}

		IEnumerable<ITreeViewDataItem> ITreeViewDataItem.Child {
			get { return new Rable(Child.GetEnumerator()); }
		}
		private struct Rable : IEnumerable<ITreeViewDataItem> {
			private IEnumerator<IFileStruct> _rator;
			public Rable(IEnumerator<IFileStruct> rator) {
				_rator = rator;
			}
			public IEnumerator<ITreeViewDataItem> GetEnumerator() {
				return new Rator(_rator);
			}

			IEnumerator IEnumerable.GetEnumerator() {
				return GetEnumerator();
			}
		}
		private struct Rator : IEnumerator<ITreeViewDataItem> {
			public Rator(IEnumerator<IFileStruct> rator) {
				_rator = rator;

			}
			private readonly IEnumerator<IFileStruct> _rator;
			public void Dispose() {
				_rator.Dispose();

			}

			public bool MoveNext() {
				return _rator.MoveNext();
			}

			public void Reset() {
				_rator.Reset();
			}

			public ITreeViewDataItem Current {
				get { return _rator.Current; }
			}

			object IEnumerator.Current {
				get { return Current; }
			}
		}
	}
}
